﻿//13. Write a program that reads a list of words from a file words.txt and finds how many times each of the words is contained in another file test.txt. The result should be written in the file result.txt and the words should be sorted by the number of their occurrences in descending order. Handle all possible exceptions in your methods.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WordOccurences
{
    class WordOccurences
    {
        static void Main()
        {
            try
            {
                StreamReader readerAll = new StreamReader("test.txt"); //Print all file
                using (readerAll)
                {
                    Console.WriteLine("The file contains:\n");
                    Console.WriteLine(readerAll.ReadToEnd());
                }
                Console.WriteLine();

                StreamReader readerWords = new StreamReader("words.txt"); //Count how many times each word occurs.
                List<int> occur = new List<int>();

                using (readerWords)
                {
                    string eachWord = readerWords.ReadLine();
                    while (eachWord != null)
                    {
                        int counter = 0;
                        StreamReader readerFile = new StreamReader("test.txt");
                        using (readerFile)
                        {
                            string line = readerFile.ReadLine();
                            while (line != null)
                            {
                                if (line.Contains(eachWord) == true)
                                {
                                    counter++;
                                }
                                line = readerFile.ReadLine();
                            }
                            occur.Add(counter);
                        }
                        eachWord = readerWords.ReadLine();
                    }
                }

                string[] wordNum = new string[occur.Count]; //Make string with each word and number of occurances
                StreamReader readerWords1 = new StreamReader("words.txt");
                using (readerWords1)
                {
                    string eachWord1 = readerWords1.ReadLine();
                    int temp = 0;
                    while (eachWord1 != null)
                    {
                        wordNum[temp] = occur[temp].ToString() + " times - " + eachWord1;

                        eachWord1 = readerWords1.ReadLine();
                        temp++;
                    }
                }
                Array.Sort(wordNum); //Sort array in descending order
                Array.Reverse(wordNum);

                Console.WriteLine("The result is:\n");

                StreamWriter writer = new StreamWriter("result.txt", false); //write the array in result file
                using (writer)
                {
                    for (int i = 0; i < wordNum.Length; i++)
                    {
                        writer.WriteLine(wordNum[i]);
                    }
                }

                StreamReader readResult = new StreamReader("result.txt"); // Print the result
                using (readResult)
                {
                    Console.WriteLine(readResult.ReadToEnd());
                }

            }
            catch (FileNotFoundException) //Handle Exceptions
            {
                Console.Error.WriteLine("Can not find file.");
            }
            catch (DirectoryNotFoundException)
            {
                Console.Error.WriteLine("Invalid directory in the file path.");
            }
            catch (IOException)
            {
                Console.Error.WriteLine("Can not open the file.");
            }
        }
    }
}
